<template>
  <el-dialog :visible.sync="showRoles" title="分配角色" @close="closeEvent">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="showRoles = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRolesPut } from '@/api/employees'
export default {
  data() {
    return {
      showRoles: false,
      checkList: [], // checkbox列表
      roleIds: [], // 角色数据回显列表
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysRole()
      this.checkList = res.data.rows
    },
    // 获取分配角色的信息
    async getList(id) {
      const res = await sysUser(id)
      this.id = id
      this.roleIds = res.data.roleIds
    },
    // 点击提交获取新的角色
    async submit() {
      await sysUserAssignRolesPut({
        id: this.id,
        roleIds: this.roleIds
      })
      this.$message.success('角色分配成功')
      this.showRoles = false
    },
    // 清空数据
    closeEvent() {
      this.roleIds = []
    }
  }
}
</script>

<style></style>
